package com.pignest.hotel.service;

import java.util.Date;
import java.util.List;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.HotelBill;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.ProtocolList;
import com.pignest.hotel.beans.ProtocolPrice;
import com.pignest.hotel.beans.ProtocolUnit;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.beans.RoomType;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.beans.TbCloseAccounts;
import com.pignest.hotel.beans.TbLiveGoodsBorrow;
import com.pignest.hotel.beans.User;
import com.pignest.hotel.dao.OtherBusiDao;
import com.pignest.hotel.definition.BillType;
import com.pignest.hotel.definition.ChargeOpType;
import com.pignest.hotel.definition.InComeItemType;
import com.pignest.hotel.definition.PayMethod;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Global;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.system.InComeUtil;
import com.pignest.hotel.util.DateUtil;

public class OtherBusiService extends BaseService {

	/**非主要业务的服务*/
	
	 OtherBusiDao otherBusiDao;
 
	 public List<HotelBill> queryHotelBill(String roomName,String name,String orderNo,String type,Integer isPrint,Date sOpTime,Date eOpTime)
	 {
		 return otherBusiDao.queryBill(roomName, name, orderNo, type, isPrint, sOpTime, eOpTime);
	 }
	 public List<HotelBill> queryHotelBillPage(String roomName,String name,String orderNo,String type,Integer isPrint,Date sOpTime,Date eOpTime,int start,int length)
	 {
		 return otherBusiDao.queryBillPage(roomName, name, orderNo, type, isPrint, sOpTime, eOpTime, start, length);
	 }
	 public HotelBill queryBillByLiving(Living l,String type)
	 {
		 return otherBusiDao.queryBillByLivingId(l,type);
	 }
	 public void savePartCheckBill(Living l,List<TbAccounts> selAcc,int price)
	 {
		 Room room =l.getRoom();
		 Date liveDate =l.getLiveTime();
		 String orderNo=DateUtil.toString(liveDate,"yyyyMMddHHmmss");
		 String roomName =room.getName();
			String roomType =room.getType().getName();
			int adjustPrice =l.getAdjustPrice();
			String name =l.getCustormName();
			String cerType =l.getPassportType();
			String cerNo =l.getPassportNumber();
			float days =Services.getIncomeService().queryCheckOutDays(l,true,selAcc);
			int deposit =Services.getIncomeService().countAllDeposit(l, false);//押金
			//TODO
			int rent =Services.getIncomeService().countRoomConsumsChecked(l, true,selAcc);//房费
			//int cost =Services.getIncomeService().countOtherConsums(l,true);//其它消费，不包括房费
			int cost =Services.getIncomeService().countTypeAccount(l, true, InComeItemType.WPXF.name(),true,selAcc);
			int clearCost =Services.getIncomeService().countTypeAccount(l, true, InComeItemType.QXF.name(),true,selAcc);
			int repaCost =Services.getIncomeService().countTypeAccount(l, true, InComeItemType.PCF.name(),true,selAcc);
			//查询总消费情况为项目为所有负数的情况
			//int accounts =Services.getIncomeService().countAllConsumsUsed(l, true,true);
			int accounts=rent+cost+clearCost+repaCost;
			//int refundment =Services.getIncomeService().countBalance(l, true);//续费金额
			int comeOn =price;
			//TbCloseAccounts ca = Services.getReceptionService().queryLastCloseAccount(l);
			TbCloseAccounts ca =l.getTbColoseAccounts();
			User user =Hotel.getCurrentUser();
			String userName =user.getName();
			HotelBill b = new HotelBill();
			b.setLiving(l);
			b.setType(BillType.PARTCHECK.name());
			b.setOrderNo(BillType.PARTCHECK.getEname()+orderNo);
			b.setRoomName(roomName);
			b.setRoomType(roomType);
			b.setRoomPrice(l.getRoom().getType().getPrice());
			b.setDeposit(deposit);
			b.setAdjustPrice(adjustPrice);
			b.setName(name);
			b.setCerType(cerType);
			b.setCerNo(cerNo);
			b.setLiveTime(l.getLiveTime());
			b.setOpenType(l.getOpenRoomType());
			//结账特有的/如果是钟点房则单位是小时
			b.setLiveCount(days);
			b.setLeaveTime(l.getLeaveDate());
			b.setRent(rent);
			b.setCost(cost);
			b.setService(clearCost);
			b.setReparation(repaCost);
			b.setAccounts(accounts);
			b.setRefundment(comeOn);
			b.setService(0);
			if(ca!=null)
			  b.setPayment(ca.getType());
			b.setOperator(userName);
			b.setOpTime(new Date());
			this.insert(b);
	 }
	 public  void saveCheckBill(Living l)
	    {
	    	Room room =l.getRoom();
			Date liveDate =l.getLiveTime();
			String orderNo=DateUtil.toString(liveDate,"yyyyMMddHHmmss");
			String roomName =room.getName();
			String roomType =room.getType().getName();
			int adjustPrice =l.getAdjustPrice();
			String name =l.getCustormName();
			String cerType =l.getPassportType();
			String cerNo =l.getPassportNumber();
			float days =Services.getIncomeService().queryCheckOutDays(l,true,null);
			int deposit =Services.getIncomeService().countAllDeposit(l, true);//押金
			int rent =Services.getIncomeService().countRoomConsumsChecked(l, true,null);//房费
			//int cost =Services.getIncomeService().countOtherConsums(l,true);//其它消费，不包括房费
			int cost =Services.getIncomeService().countTypeAccount(l, true, InComeItemType.WPXF.name(),true,null);
			int clearCost =Services.getIncomeService().countTypeAccount(l, true, InComeItemType.QXF.name(),true,null);
			int repaCost =Services.getIncomeService().countTypeAccount(l, true, InComeItemType.PCF.name(),true,null);
			//查询总消费情况为项目为所有负数的情况
			int accounts =Services.getIncomeService().countAllConsumsUsed(l, true,true);
			int refundment =Services.getIncomeService().countBalance(l, true);//余额
			//TbCloseAccounts ca = Services.getReceptionService().queryLastCloseAccount(l);
			TbCloseAccounts ca =l.getTbColoseAccounts();
			User user =Hotel.getCurrentUser();
			String userName =user.getName();
			HotelBill b = new HotelBill();
			b.setLiving(l);
			b.setType(BillType.CHECK.name());
			b.setOrderNo(BillType.CHECK.getEname()+orderNo);
			b.setRoomName(roomName);
			b.setRoomType(roomType);
			b.setRoomPrice(l.getRoom().getType().getPrice());
			b.setDeposit(deposit);
			b.setAdjustPrice(adjustPrice);
			b.setName(name);
			b.setCerType(cerType);
			b.setCerNo(cerNo);
			b.setLiveTime(l.getLiveTime());
			b.setOpenType(l.getOpenRoomType());
			//结账特有的/如果是钟点房则单位是小时
			b.setLiveCount(days);
			b.setLeaveTime(l.getLeaveDate());
			b.setRent(rent);
			b.setCost(cost);
			b.setService(clearCost);
			b.setReparation(repaCost);
			b.setAccounts(accounts);
			b.setRefundment(refundment);
			b.setService(0);
			if(ca!=null)
			  b.setPayment(ca.getType());
			b.setOperator(userName);
			b.setOpTime(new Date());
			this.insert(b);
	    }
	 public void saveLivingBill(Living l,String payment)
	 {
		 Room room =l.getRoom();
		 Date liveDate =l.getLiveTime();
		 String orderNo=DateUtil.toString(liveDate,"yyyyMMddHHmmss");
		 String roomName =room.getName();
		 String roomType =room.getType().getName();
		 int adjustPrice =l.getAdjustPrice();
		 String name =l.getCustormName();
		 String cerType =l.getPassportType();
		 String cerNo =l.getPassportNumber();
		 int deposit =Services.getIncomeService().countAllDeposit(l, false);

		 User user =Hotel.getCurrentUser();
		 String userName =user.getName();
		 
		 HotelBill b = new HotelBill();
			b.setLiving(l);
			b.setType(BillType.LIVING.name());
			b.setOrderNo(BillType.LIVING.getEname()+orderNo);
			b.setRoomName(roomName);
			b.setRoomType(roomType);
			b.setPayment(payment);
			b.setRoomPrice(l.getRoom().getType().getPrice());
			b.setDeposit(deposit);
			b.setAdjustPrice(adjustPrice);
			b.setName(name);
			b.setCerType(cerType);
			b.setCerNo(cerNo);
			b.setLiveTime(l.getLiveTime());
			b.setOpenType(l.getOpenRoomType());
			b.setOperator(userName);
			b.setOpTime(new Date());
			this.insert(b);
	 }
	 public void saveComeOnBill(Living l,int comeOnMoney){
		    Room room =l.getRoom();
			Date liveDate =l.getLiveTime();
			String orderNo=DateUtil.toString(liveDate,"yyyyMMddHHmmss");
			String roomName =room.getName();
			String roomType =room.getType().getName();
			String cerType =l.getPassportType();
			String cerNo =l.getPassportNumber();
			String name =l.getCustormName();
			User user =Hotel.getCurrentUser();
			String userName =user.getName();
			 HotelBill b = new HotelBill();
				b.setLiving(l);
				b.setType(BillType.DEPOSIT.name());
				b.setOrderNo(BillType.DEPOSIT.getEname()+orderNo);
				b.setRoomName(roomName);
				b.setRoomType(roomType);
				b.setPayment(PayMethod.CASH.name());
				b.setRoomPrice(l.getRoom().getType().getPrice());
				b.setDeposit(comeOnMoney);
				b.setAdjustPrice(l.getAdjustPrice());
				b.setName(name);
				b.setCerType(cerType);
				b.setCerNo(cerNo);
				b.setLiveTime(l.getLiveTime());
				b.setOpenType(l.getOpenRoomType());
				b.setOperator(userName);
				b.setOpTime(new Date());
				this.insert(b);
	 }
	 public void saveChangeRoomBill(Living l,String oldRoomName,int price,String remark)
	 {
		    Room room =l.getRoom();
			Date liveDate =l.getLiveTime();
			String orderNo=DateUtil.toString(liveDate,"yyyyMMddHHmmss");
			String roomName =room.getName();
			String roomType =room.getType().getName();
			String cerType =l.getPassportType();
			String cerNo =l.getPassportNumber();
			String name =l.getCustormName();
			User user =Hotel.getCurrentUser();
			String userName =user.getName();
			HotelBill b = new HotelBill();
			 b.setLiving(l);
			 b.setType(BillType.CHANGEROOM.name());
			 b.setOrderNo(BillType.CHANGEROOM.getEname()+orderNo);
			 b.setRoomName(roomName);
			 b.setRoomType(roomType);
			 b.setPayment(PayMethod.CASH.name());
			 b.setRoomPrice(l.getRoom().getType().getPrice());
			 b.setAccounts(price);
			 b.setOldRoomName(oldRoomName);
			 b.setRemark(remark);
				//b.setDeposit(comeOnMoney);
			 b.setAdjustPrice(l.getAdjustPrice());
			 b.setName(name);
			 b.setCerType(cerType);
			 b.setCerNo(cerNo);
			 b.setLiveTime(l.getLiveTime());
			 b.setOpenType(l.getOpenRoomType());
			 b.setOperator(userName);
			 b.setOpTime(new Date());
			this.insert(b);
	 }
	 public void saveCancelBill(Living l,PayMethod p,int price,String remark)
	 {
		    Room room =l.getRoom();
			Date liveDate =l.getLiveTime();
			String orderNo=DateUtil.toString(liveDate,"yyyyMMddHHmmss");
			String roomName =room.getName();
			String roomType =room.getType().getName();
			String cerType =l.getPassportType();
			String cerNo =l.getPassportNumber();
			String name =l.getCustormName();
			User user =Hotel.getCurrentUser();
			String userName =user.getName();
			HotelBill b = new HotelBill();
			 b.setLiving(l);
			 b.setType(BillType.CANCEL.name());
			 b.setOrderNo(BillType.CANCEL.getEname()+orderNo);
			 b.setRoomName(roomName);
			 b.setRoomType(roomType);
			 b.setPayment(p.name());
			 b.setRoomPrice(l.getRoom().getType().getPrice());
			 b.setAccounts(price);
			 b.setRemark(remark);
				//b.setDeposit(comeOnMoney);
			 b.setAdjustPrice(l.getAdjustPrice());
			 b.setName(name);
			 b.setCerType(cerType);
			 b.setCerNo(cerNo);
			 b.setLiveTime(l.getLiveTime());
			 b.setOpenType(l.getOpenRoomType());
			 b.setOperator(userName);
			 b.setOpTime(new Date());
			this.insert(b);
	 }
	 public List<TbLiveGoodsBorrow> getGoodBorrow(String LivingId,String goodId){
		 StringBuffer hql = new StringBuffer();
		 hql.append(" from TbLiveGoodsBorrow b where 1=1 and b.living.id='").append(LivingId).append("'");
		 hql.append(" and b.tbGoods.id='").append(goodId).append("'");
		 List<TbLiveGoodsBorrow> list =this.find(hql.toString());
		 return list;
	 }
	 public void insertProtocol(ProtocolUnit unit){
		 this.insert(unit);
		 List<RoomType> types =Services.getRoomService().listAll(RoomType.class);
		 if(types!=null)
		 for(RoomType r:types)
		 {
			 ProtocolPrice price = new ProtocolPrice();
			 price.setProtocolUnit(unit);
			 price.setRoomType(r);
			 price.setPrice(r.getPrice());
			 price.setOpTime(new Date());
			 price.setOpUser(Hotel.getCurrentUser().getName());
			 this.insert(price);
		 }
	 }
	 public void deleteProtocol(String protocolId)
	 {
		 ProtocolUnit u =this.get(ProtocolUnit.class, protocolId);
		 if(u.getPrice()>0){
			 return ;
		 }
		 List<ProtocolPrice> list =queryProtocolPrice(protocolId);
		 if(list!=null){
			 for(ProtocolPrice p:list )
			 {
				 this.delete(p);
			 }
		 }
		 
		 this.delete(u);
	 }
	 public void changeOpenRoom(Living l){
		 this.update(l);
		 HotelBill oB =queryBillByLiving(l,BillType.LIVING.name());
		 saveLivingBill(l,oB.getPayment());
	 }
	 /**挂账调用*/
	 public void insertHangUp(Living l,ProtocolUnit u,int price){
		 ProtocolList pl =new ProtocolList();
		 pl.setProtocolUnit(u);
		 pl.setLiving(l);
		 pl.setPrice(price);
		 pl.setRoomName(l.getRoom().getName());
		 pl.setName(l.getCustormName());
		 pl.setAdjustPrice(l.getAdjustPrice());
		 pl.setLivingTime(l.getLiveTime());
		 pl.setLeaveTime(l.getLeaveDate());
		 pl.setState(ProtocolList.HANGUP_UNCHECK);
		 pl.setOpenName(l.getOpenRoomType());
		 pl.setOpTime(new Date());
		 pl.setOpUser(Hotel.getCurrentUser().getName());
		 this.insert(pl);
		 int allPrice =otherBusiDao.countUnCheckHangUpMoney(u.getId());
		 u.setPrice(allPrice);
		 this.update(u);
		 
	 }
	 public void saveCheckHangUp(ProtocolUnit unit,PayMethod payMethod)
	 {
		 List<ProtocolList> list =queryUnCheckProtocolList(unit.getId());
		 if(list==null)
			 return ;
		 int income =otherBusiDao.countUnCheckHangUpMoney(unit.getId());
		 String orderNo=DateUtil.toString(new Date(), "yyyyMMddHHmmss");
		 String serialNumber=Global.getLivineSerialNumber();
		 TbCloseAccounts ca = new TbCloseAccounts();
			ca.setLiving(null);
			ca.setInvoice(0);
			ca.setTime(new Date());
			ca.setAccounts(income);
			ca.setType(payMethod.name());
			ca.setUsername(Hotel.getCurrentUser().getName());
			ca.setUserId(Hotel.getCurrentUser().getId());
			ca.setOrderNo(orderNo);
			ca.setSerialNumber(serialNumber);
			ca.setCheckOutNo(serialNumber);
			ca.setNeedAccounts(income);
		 this.insert(ca);
		 for(ProtocolList pl:list)
		 {
			// income=income+pl.getPrice();
			 pl.setCheckTime(new Date());
			 pl.setOpUser(Hotel.getCurrentUser().getName());
			 pl.setState(ProtocolList.HANGUP_CHECK);
			 Living living =pl.getLiving();
			 if(living!=null){
				 List<TbAccounts> acc=Services.getIncomeService().queryAccountsBySigleLiving(living);
				 if(acc!=null){
					//更新账单
					 for(TbAccounts a:acc){
						 a.setEndstate("是");
				         a.setEndtime(new Date());
				         this.update(a);
					 }
				 }
				 int consum =Services.getIncomeService().countAllConsums(living,true,true);
				 living.setTbColoseAccounts(ca);
				 living.setStorage(consum);
				 living.setUsed(consum);
				 living.setAvailable(0);
				 this.update(living);
				 this.saveCheckBill(living);
			 }
			 this.update(pl);
		 }
		 int allPrice =otherBusiDao.countUnCheckHangUpMoney(unit.getId());
		 ProtocolUnit nUnit =get(ProtocolUnit.class,unit.getId());
		 nUnit.setPrice(allPrice);
		 this.update(nUnit);
		 
		 if(Hotel.onDuty()){
			 Hotel.receive(income, ChargeOpType.OnConsume, payMethod,"挂账"+Utils.toPrice(income));
		 }
	 }
	 public List<ProtocolUnit> queryProtocolUnit(String protocolNo,String company,String contactName,Boolean isHangUp){
		 return otherBusiDao.queryProtocolUnit(protocolNo, company, contactName, isHangUp);
	 } 
	 public List<ProtocolPrice> queryProtocolPrice(String protocolId){
		 return otherBusiDao.queryProtocolPrice(protocolId);
	 }
	 public ProtocolPrice queryProtocolPrice(String protocolId,String roomTypeId){
		 return otherBusiDao.queryProtocolPrice(protocolId,roomTypeId);
	 }
	 public List<ProtocolList> queryProtocolList(String protocolId,String livingId){
		 return otherBusiDao.queryProtocolList(protocolId, livingId);
	 } 
	 public List<ProtocolList> queryUnCheckProtocolList(String protocolId){
		 return otherBusiDao.queryUnCheckProtocolList(protocolId);
	 }
	public OtherBusiDao getOtherBusiDao() {
		return otherBusiDao;
	}

	public void setOtherBusiDao(OtherBusiDao otherBusiDao) {
		this.otherBusiDao = otherBusiDao;
	}
	
	
	
}
